﻿<%@ Page Language="C#" AutoEventWireup="true" %>

<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="Image=System.Drawing.Image" %>
<%@ Import Namespace="Infrastructure" %>

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        string code = ReqHelper.GetValue("id");
        if (string.IsNullOrWhiteSpace(code)) { return; }
        bool isUrl = false;
        if (!string.IsNullOrWhiteSpace(ReqHelper.GetValue("isUrl")))
        {
            bool.TryParse(ReqHelper.GetValue("isUrl"), out isUrl);
        }
        int width = ReqHelper.GetIntValue("width");
        int height = ReqHelper.GetIntValue("height");
        using (MemoryStream ms = new MemoryStream())
        {
            QrCreater creater = new QrCreater();
            Image imgCode = creater.CreateImage(code, isUrl, width, height);
            imgCode.Save(ms, ImageFormat.Png);
            Response.ContentType = "image/png";
            Response.OutputStream.Write(ms.GetBuffer(), 0, (int)ms.Length);
        }
    }

    private class ReqHelper
    {
        public static string GetValue(string key)
        {
            var value = HttpContext.Current.Request[key];
            return value == null ? string.Empty : value.Trim();
        }

        public static int GetIntValue(string key)
        {
            var value = GetValue(key);
            int result = 0;
            int.TryParse(value, out result);
            return result;
        }
    }
</script>
